package com.iemes.controller.warehouse;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.ProductreturnFormMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.warehouse.ProductreturnMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;
import com.iemes.util.MyDateUtils;

@RequestMapping("/returnproduct_statistics/")
@Controller
public class Returnproduct_StatisticsController extends BaseController {

	@Inject
	private ProductreturnMapper productreturnMapper;

	// 查看退货产品入库数量统计表，显示按钮。
	@RequestMapping("returnproduct_statistics")
	public String returnproduct_statistics(Model model) {
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/warehouse/returnproduct_statistics_list";
	}

	// 退货产品入库数量统计表分页
	@ResponseBody
	@RequestMapping("returnproduct_statistics_findByPage")
	public PageView findByPage(String pageNow, String pageSize) throws Exception {
		ProductreturnFormMap productreturnFormMap=getFormMap(ProductreturnFormMap.class);
		List<Map<String, Object>> statistics=productreturnMapper.getAllStatistics(productreturnFormMap);
		for(Map<String, Object>map:statistics){
			String date=map.get("statisticsDate").toString();
			//jsp的sStorageDay显示换成intervalTime
			map.put("sStorageDay", MyDateUtils.intervalTime(date,0));
		}
		LuogicPaging(pageNow, pageSize, statistics);
		return pageView;
	}

	// 编辑退货产品入库数量统计表
	@RequestMapping("returnproduct_statistics_edit")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		if(Common.isNotEmpty(id)){
			//根据id获得退货产品入库数量统计表
			ProductreturnFormMap statistics=productreturnMapper.getByStatisticsId(id);
			model.addAttribute("statistics",statistics);
		}
		return Common.BACKGROUND_PATH + "/warehouse/returnproduct_statistics_edit";
	}

	// 修改退货产品入库数量统计表
	@ResponseBody
	@RequestMapping(value = "returnproduct_statistics_editEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module = "退货产品入库数量统计表", methods = "查看退货产品入库数量统计表-修改退货产品入库数量统计表") // 凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity(HttpServletRequest request) throws Exception {
		ProductreturnFormMap productreturnFormMap = getFormMap(ProductreturnFormMap.class);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		// 获取request
		request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();  
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap)Common.findUserSession(request);
		productreturnFormMap.put("statisticsDate", sdf.format(date));
		productreturnFormMap.put("fillPeopleId", userFormMap.getInt("id"));
		baseMapper.editEntity(productreturnFormMap);
		return "success";
	}	
}